package com.panshi.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.panshi.modules.domain.SysMenu;
import com.panshi.modules.domain.SysRole;
import com.panshi.modules.domain.SysUser;
import com.panshi.modules.mapper.SysMenuMapper;
import com.panshi.modules.mapper.SysRoleMapper;
import com.panshi.modules.mapper.SysUserMapper;
import com.panshi.modules.service.SysUserService;
import com.panshi.vo.SysUserVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.beans.IntrospectionException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 857
 * @since 2020-07-29
 */
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysMenuMapper sysMenuMapper;


    /**
     * 查看系统用户 list
     * @return
     */
    @Cacheable(value = "system:sysUser",key="#root.methodName")
    public List<SysUserVO> listSysUser(){
        List<SysUser> sysUsers = sysUserMapper.selectList(null);
        if(CollectionUtils.isEmpty(sysUsers)){
            return new ArrayList<>();
        }
        List<SysUserVO> list = new ArrayList<>();
        for (SysUser user : sysUsers){
            SysUserVO userVO = new SysUserVO();
            BeanUtils.copyProperties(user , userVO);
            list.add(userVO);
        }
        return list;
    }

    /**
     * 分页
     * @param page
     * @return
     */
    public IPage<SysUser> pageUserList(IPage<SysUser> page){
        IPage<SysUser> sysUserIPage = sysUserMapper.selectPage(page, null);
        return sysUserIPage;
    }

    /**
     * 通过ID查询系统用户
     * @param id
     * @return
     */
    @Override
    public SysUserVO getSysUserById(Long id) {
        SysUser user = sysUserMapper.selectById(id);
        SysUserVO sysUserVO = new SysUserVO();
        BeanUtils.copyProperties(user,sysUserVO);
        return sysUserVO;
    }

    /**
     * 通过账号(username)查询系统用户
     * @param username
     * @return
     */
    @Override
    public SysUserVO getSysUserByUsername(String username) {
        SysUser sysUser = sysUserMapper.selectOne(new QueryWrapper<SysUser>().eq("username",username).select("id","username","password"));

        SysUserVO userVO = null;
        if(null != sysUser){

            List<SysRole> sysRoles = sysRoleMapper.listSysRolesByUserId(sysUser.getId());
            if(!CollectionUtils.isEmpty(sysRoles)){

                sysUser.setSysRoles(sysRoles);
                List<Integer> collect = sysRoles.stream().map(SysRole::getId).collect(Collectors.toList());
                if(!CollectionUtils.isEmpty(collect)){

                    List<SysMenu> sysMenus = sysMenuMapper.listSysMenuByRoleIds(collect);
                    sysUser.setSysMenus(sysMenus);
                }
            }
            userVO = new SysUserVO();
            BeanUtils.copyProperties(sysUser, userVO);
        }

        return userVO;
    }


    /**
     * 新增
     * @param sysUserVO
     * @return
     */
    @Override
    public int saveSysUser(SysUserVO sysUserVO) {
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(sysUserVO,sysUser);
        int result = sysUserMapper.insert(sysUser);

        //user-role关系表维护
        String roleIds = sysUserVO.getRoleIds();
        if(StringUtils.isNotBlank(roleIds)){
            //写入用户-角色关系表
            List<String> listRoleIds = Arrays.asList(sysUserVO.getRoleIds().split(","));
            sysUserMapper.saveUserRoleRelas(sysUser.getId(), listRoleIds);
        }

        return result;
    }

    /**
     * 修改系统用户
     * @param sysUserVO
     * @return
     */
    @Override
    public int updateSysUser(SysUserVO sysUserVO) {
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(sysUserVO,sysUser);
        int result = sysUserMapper.updateById(sysUser);

        String roleIds = sysUserVO.getRoleIds();

        if(StringUtils.isNotBlank(roleIds)){
            //先删除所有
            sysUserMapper.deleteUserRolesByUserId(sysUserVO.getId());
            //再将新的关系写入
            List<String> listRoleIds = Arrays.asList(roleIds.split(","));
            sysUserMapper.saveUserRoleRelas(sysUser.getId(),listRoleIds);
        }

        return result;
    }


    /**
     * 删除系统用户 ( byId)
     * @param id
     * @return
     */
    @Override
    public int deleteSysUser(Integer id){

        int result = sysUserMapper.deleteById(id);
        //删除用户对应的角色关系
        sysUserMapper.deleteUserRolesByUserId(id);
        return result;
    }


    @Override
    public int selectCountByUsername(String username, Integer id) {
        return sysUserMapper.selectCount(new QueryWrapper<SysUser>().eq("username",username).ne("id",id));
    }


}
